package com.crl.ssc.dic.controller;

import com.cars.util.bean.BasePage;
import com.cars.util.global.GlobalOperaCode;
import com.cars.util.global.GlobalReturnCode;
import com.cars.util.json.JsonResult;
import com.cars.util.json.JsonResultUtil;
import com.cars.util.string.StringUtil;
import com.cars.util.validation.UpdateValidGroup;
import com.crl.ssc.dic.dto.DicCzDTO;
import com.crl.ssc.dic.service.DicCzService;
import com.crl.ssc.dic.vo.DicCzSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @description: 字典管理-车站字典API
 * @author:  孔垂云
 * @version: 1.0
 * @date: 2020-07-26 23:24:46
 */
@RestController
@RequestMapping("/dic/cz")
@Api(tags = "字典管理-车站字典")
public class DicCzController {

    @Autowired
    private DicCzService dicCzService;

    /**
     * 车站字典列表
     *
     * @param dicCzSearchVO
     * @return
     */
    @PostMapping("/list")
    @ApiOperation(value = "车站字典列表")
    public JsonResult list(@RequestBody DicCzSearchVO dicCzSearchVO) {
        int count = dicCzService.count(dicCzSearchVO);
        List<DicCzDTO> list = dicCzService.list(dicCzSearchVO);
        BasePage<List<DicCzDTO>> basePage = new BasePage(count, list);
        return JsonResultUtil.ok(basePage);
    }

    /**
     * 新增车站字典
     *
     * @param dicCzDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增车站字典")
    public JsonResult add(@RequestBody @Valid DicCzDTO dicCzDTO) {
        int flag = dicCzService.add(dicCzDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 修改车站字典
     *
     * @param dicCzDTO
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改车站字典")
    public JsonResult update(@Validated({UpdateValidGroup.class}) @RequestBody DicCzDTO dicCzDTO) {
        int flag = dicCzService.update(dicCzDTO);
        if (flag == 1) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 删除车站字典
     *
     * @param map
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除车站字典")
    public JsonResult delete(@RequestBody Map<String, String> map) {
        String czdm = map.get("czdm");
        if (StringUtil.isNullOrEmpty(czdm)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        int flag = dicCzService.delete(czdm);
        if (flag == GlobalOperaCode.OPERA_SUCCESS) {
            return JsonResultUtil.ok();
        } else {
            return JsonResultUtil.error(GlobalReturnCode.SERVER_ERROR);
        }
    }
    /**
     * 获取单个车站字典
     *
     * @param map
     * @return
     */
    @PostMapping("/get")
    @ApiOperation(value = "获取单个车站字典")
    public JsonResult get(@RequestBody Map<String, String> map) {
        String czdm = map.get("czdm");
        if (StringUtil.isNullOrEmpty(czdm)) {
            JsonResultUtil.error(GlobalReturnCode.PARAMS_ERROR);
        }
        DicCzDTO dicCzDTO = dicCzService.get(czdm);
        return JsonResultUtil.ok(dicCzDTO);
    }
}
